package com.jz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Hasee on 2022-03-07.
 */
@RestController
public class LoginController {
    @Autowired
    private RedisTemplate redisTemplate;
    //登陆的时候要先把redis的key存储到cookie,然后存储到redis中
    @GetMapping("login")
    public String login(HttpServletResponse response){
        UserVo userVo = new UserVo();
        userVo.setName("zhangsan");
        userVo.setAge(18);
        //查询数据库，判断是否存在这个用户
        //查到了，允许登陆
        String key ="login"+userVo.getName();
        //把key存储到cookie中
        Cookie userCookie=new Cookie("loginInfo",key);
        response.addCookie(userCookie);
        redisTemplate.opsForValue().set(key,userVo);
        return "SUCCESS";
    }
}
